<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 *
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfGuardUser.php 9999 2008-06-29 21:24:44Z fabien $
 */
class sfGuardUser extends PluginsfGuardUser {

      public function __toString()
      {
         
                       return $this->first_name;
      }
	
	public function getAccount() {
		
		if($this->account=='')return 0;
		return $this->account;
	}
	
	public function getLinkAvatar() {
		
		if ($this->photo == '') {
			if ($this->getGender () == 'm') {
				$str = link_to ( image_tag ( '/images/no_avatar_man.jpg' ,'width="50"'), '@show_profile?id=' . $this->getId () );
			
			} else {
				$str = link_to ( image_tag ( '/images/no_avatar_woman.jpg' ,'width="50"'), '@show_profile?id=' . $this->getId () );
				if ($this->is_vip == true)
					$str .= '<br>VIP lady';
			}
		} else {
			$str = link_to ( image_tag ( '/uploads/users/avatar/_thumbnail/' . $this->getPhoto (), 'title="' . $this . '"' ), '@show_profile?id=' . $this->getId () );
			if ($this->is_vip == true)
				$str .= '<br>VIP lady';
		}
		
		return $str;
	}
	
	public function getLinkAvatarBig($height=150,$_target='_self') {
		
		///Передаем массивы стран 
		$arrc = sfCultureInfo::getInstance ( sfContext::getInstance ()->getUser ()->getCulture () )->getCountries ();
		////
		

		$title = $this . ' ' . $arrc [$this->getCountry ()] . ' ' . $this->getAge () . '/' . $this->getHeight ();
		
		if ($this->photo == '' or $this->photo == 'Array') {
			if ($this->getGender () == 'm') {
				$str = link_to ( image_tag ( '/images/no_avatar_man.jpg',array('title'=>$title)), '@show_profile?id=' . $this->getId (), array ('class' => 'img_link', 'title' => $title, 'target'=>$_target ) );
			} else {
				$str = link_to ( image_tag ( '/images/no_avatar_woman.jpg',array('title'=>$title)), '@show_profile?id=' . $this->getId (), array ('class' => 'img_link', 'title' => $title, 'target'=>$_target  ) );
			}
		} else {
			$str = link_to ( image_tag ( '/uploads/users/avatar/thumbnail/' . $this->getPhoto (), array('title'=>$title) ), '@show_profile?id=' . $this->getId (), array ('class' => 'img_link', 'title' => $title , 'target'=>$_target ) );
		}
		
		return $str;
	}
	
	
	public function getUrlAvatar() {
		
		if ($this->photo == '' or $this->photo == 'Array') {
			if ($this->getGender () == 'm') {
				$str = '/images/no_avatar_man.jpg';
			} else {
				$str = '/images/no_avatar_woman.jpg';
			}
		} else {
			$str = '/uploads/users/avatar/thumbnail/' . $this->getPhoto ();
		}
		
		return $str;
	}
	
	
	public function getAvatar($class='') {
		
		///Передаем массивы стран 
		$arrc = sfCultureInfo::getInstance ( sfContext::getInstance ()->getUser ()->getCulture () )->getCountries ();
		////
		
		
		$title = $this . ' ' . $arrc [$this->getCountry ()] . ' ' . $this->getAge () . '/' . $this->getHeight ();		
		
		if ($this->photo == '') {
			if ($this->getGender () == 'm') {
				$str = image_tag ( '/images/no_avatar_man.jpg', 'class="'.$class.'"');
			} else {
				$str = image_tag ( '/images/no_avatar_woman.jpg', 'class="'.$class.'"');
			}
		} else {
			$str = image_tag ( '/uploads/users/avatar/thumbnail/' . $this->getPhoto (), 'title="' . $title . '" class="'.$class.'"' );
		}
		
		return $str;
	}
	
	public function getAvatarSmall() {
		
		if ($this->photo == '') {
			if ($this->getGender () == 'm') {
				$str = image_tag ( '/images/no_avatar_man_small.jpg', 'width="60"' );
			} else {
				$str = image_tag ( '/images/no_avatar_woman_small.jpg', 'width="60"' );
			}
		} else {
			$str = image_tag ( '/uploads/users/avatar/_thumbnail/' . $this->getPhoto (), 'title="' . $this . '"' );
		}
		
		return $str;
	}
	
	public function getAge() {
		
		if($this->age) return $this->age;
		if($this->getBirthday ()!='')
		{
		$t1 = strtotime ( "now" );
		$t2 = strtotime ( $this->getBirthday () );
		$r = $t1 - $t2;
		$r = ((($r / 60) / 60) / 24) / 360;
		
		return floor ( $r );
		}
		else 
		return '';
	}
	
	public function getGenderStr() {
		if ($this->getGender () == 'm') {
			return __ ( 'мужчина' );
		} else {
			return __ ( 'женщина' );
		}
		
		return floor ( $r );
	}
	
	public function getHaveChildStr() {
		if ($this->getHaveChild () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getDrinkingStr() {
		if ($this->getDrinking() == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}	

	public function getSmokingStr() {
		if ($this->getSmoking() == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}	
	
	public function getWantsChildrenStr() {
		if ($this->getWantsChildren() == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}	
	
	
	
	public function getIsVipStr() {
		if ($this->getIsVip () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getIsTopStr() {
		if ($this->getIsVip () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getIsAbonStr() {
		if ($this->getIsAbon () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getHaveChildPartnerStr() {
		if ($this->getHaveChild () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getIsPhotoToStr() {
		if ($this->getIsPhoto () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getIsVideoToStr() {
		if ($this->getIsVideo () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getIsActiveToStr() {
		if ($this->getIsActive () == true) {
			return __ ( 'да' );
		} else {
			return __ ( 'нет' );
		}
	}
	
	public function getAgeToStr($Age) {
		if (($Age >= 5) && ($Age <= 14))
			$str = __("лет");
		else {
			$num = $Age - (floor ( $Age / 10 ) * 10);
			
			if ($num == 1) {
				$str = __ ( "год" );
			} elseif ($num == 0) {
				$str = __ ( "лет" );
			} elseif (($num >= 2) && ($num <= 4)) {
				$str = __ ( "года" );
			} elseif (($num >= 5) && ($num <= 9)) {
				$str = __ ( "лет" );
			}
		}
		return $str;
	}
	
	public function getCountAll() {
		$c = new Criteria ( );
		$c->add ( FavoritePeer::FAV_ID, $this->id );
		$cnt = FavoritePeer::doCount ( $c );
		return $cnt;
	}
	
	public function getCountAllMessage() {
		$c = new Criteria ( );
		$c->add ( FavoritePeer::FAV_ID, $this->id );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::USER_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::FAV_ID );
		$c->add ( MailboxPeer::DEL_RESIVER, false );
		$cnt2 = MailboxPeer::doCount ( $c );
		
		$c = new Criteria ( );
		$c->add ( FavoritePeer::FAV_ID, $this->id );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::FAV_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::USER_ID );
		$c->add ( MailboxPeer::DEL_SENDER, false );
		$cnt = MailboxPeer::doCount ( $c );
		
		return $cnt + $cnt2;
	}
	
	public function getCountFav() {
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$cnt = FavoritePeer::doCount ( $c );
		return $cnt;
	}
	
	public function getCountFavMessage() {
		
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::USER_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::FAV_ID );
		$c->add ( MailboxPeer::DEL_SENDER, false );
		$cnt2 = MailboxPeer::doCount ( $c );
		
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::FAV_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::USER_ID );
		$c->add ( MailboxPeer::DEL_RESIVER, false );
		$cnt = MailboxPeer::doCount ( $c );
		
		return $cnt + $cnt2;
	}
	
	public function getCountBlack() {
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( FavoritePeer::FAV_ID, BlacklistPeer::BLACK_ID );
		$cnt = BlacklistPeer::doCount ( $c );
		return $cnt;
	}
	
	public function getCountBlackMessage() {
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( FavoritePeer::FAV_ID, BlacklistPeer::BLACK_ID );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::USER_ID );
		$c->addJoin ( MailboxPeer::TO_ID, BlacklistPeer::BLACK_ID );
		$c->add ( MailboxPeer::DEL_SENDER, false );
		$cnt2 = MailboxPeer::doCount ( $c );
		
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( FavoritePeer::FAV_ID, BlacklistPeer::BLACK_ID );
		$c->addJoin ( MailboxPeer::FROM_ID, BlacklistPeer::BLACK_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::USER_ID );
		$c->add ( MailboxPeer::DEL_RESIVER, false );
		$cnt = MailboxPeer::doCount ( $c );
		
		return $cnt2 + $cnt;
	}
	
	public function getCountOtpusk() {
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( sfGuardUserPeer::ID, FavoritePeer::FAV_ID );
		$c->addAnd ( sfGuardUserPeer::IS_OTPUSK, true );
		$cnt = FavoritePeer::doCount ( $c );
		return $cnt;
	}
	
	public function getCountOtpuskMessage() {
		
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( sfGuardUserPeer::ID, FavoritePeer::FAV_ID );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::USER_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::FAV_ID );
		$c->add ( MailboxPeer::DEL_SENDER, false );
		$c->addAnd ( sfGuardUserPeer::IS_OTPUSK, true );
		$cnt2 = MailboxPeer::doCount ( $c );
		
		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id );
		$c->addJoin ( sfGuardUserPeer::ID, FavoritePeer::FAV_ID );
		$c->addJoin ( MailboxPeer::FROM_ID, FavoritePeer::FAV_ID );
		$c->addJoin ( MailboxPeer::TO_ID, FavoritePeer::USER_ID );
		$c->add ( MailboxPeer::DEL_RESIVER, false );
		$c->addAnd ( sfGuardUserPeer::IS_OTPUSK, true );
		$cnt = MailboxPeer::doCount ( $c );
		
		return $cnt2 + $cnt;
	
	}
	
	public function getCountFromToMessage($from_id, $to_id) {
		$c = new Criteria ( );
		
		$crit1 = $c->getNewCriterion ( MailboxPeer::FROM_ID, $from_id );
		$crit2 = $c->getNewCriterion ( MailboxPeer::TO_ID, $to_id );
		$crit22 = $c->getNewCriterion ( MailboxPeer::DEL_RESIVER, false );
		$crit1->addAnd ( $crit2 );
		$crit1->addAnd ( $crit22 );
		$c->add ( $crit1 );
		
		$crit3 = $c->getNewCriterion ( MailboxPeer::FROM_ID, $to_id );
		$crit4 = $c->getNewCriterion ( MailboxPeer::TO_ID, $from_id );
		$crit33 = $c->getNewCriterion ( MailboxPeer::DEL_SENDER, false );
		$crit3->addAnd ( $crit4 );
		$crit3->addAnd ( $crit33 );
		$c->addOr ( $crit3 );
		
		$c->add ( MailboxPeer::PUB, true );
		
		$cnt = MailboxPeer::doCount ( $c );
		return $cnt;
	}
	
	public function getMyReadStr() {
		$arr = myTools::db_array_field ( $this->my_read );
		$values = myTools::getMyRead ();
		$new = array ();
		if (sizeof ( $arr ) > 0) {
			foreach ( $arr as $ln ) {
				if (isset ( $values [$ln] ) && ! empty ( $values [$ln] ))
					$new [] = $values [$ln];
			}
			return implode ( ', ', $new );
		} else {
			return __ ( 'информация отсутствует' );
		}
	}
	
	public function getMyRestStr() {
		$arr = myTools::db_array_field ( $this->my_rest );
		$values = myTools::getMyRest ();
		$new = array ();
		if (sizeof ( $arr ) > 0) {
			foreach ( $arr as $ln ) {
				if (isset ( $values [$ln] ) && ! empty ( $values [$ln] ))
					$new [] = $values [$ln];
			}
			return implode ( ', ', $new );
		} else {
			return __ ( 'информация отсутствует' );
		}
	}
	
	public function getMyMusikStr() {
		$arr = myTools::db_array_field ( $this->my_musik );
		$values = myTools::getMyMusik ();
		$new = array ();
		if (sizeof ( $arr ) > 0) {
			foreach ( $arr as $ln ) {
				if (isset ( $values [$ln] ) && ! empty ( $values [$ln] ))
					$new [] = $values [$ln];
			}
			return implode ( ', ', $new );
		} else {
			return __ ( 'информация отсутствует' );
		}	
	}
	
	public function getMyFoodStr() {
		$arr = myTools::db_array_field ( $this->my_food );
		$values = myTools::getMyFood ();
		$new = array ();
		if (sizeof ( $arr ) > 0) {
			foreach ( $arr as $ln ) {
				if (isset ( $values [$ln] ) && ! empty ( $values [$ln] ))
					$new [] = $values [$ln];
			}
			return implode ( ', ', $new );
		} else {
			return __ ( 'информация отсутствует' );
		}
	}
	
	public function getMySportstr() {
		$arr = myTools::db_array_field ( $this->my_sport );
		$values = myTools::getMySport ();
		$new = array ();
		if (sizeof ( $arr ) > 0) {
			foreach ( $arr as $ln ) {
				if (isset ( $values [$ln] ) && ! empty ( $values [$ln] ))
					$new [] = $values [$ln];
			}
			return implode ( ', ', $new );
		} else {
			return __ ( 'информация отсутствует' );
		}
	}
	
	public function getMyHarakterstr() {
		$arr = myTools::db_array_field ( $this->my_harakter );
		if($this->gender=='m')
		{
		$values = myTools::getMyHarakterMan ();
		 }
		else {
			$values = myTools::getMyHarakterWoman();
		}
		$new = array ();
		if (sizeof ( $arr ) > 0) {
			foreach ( $arr as $ln ) {
				if (isset ( $values [$ln] ) && ! empty ( $values [$ln] ))
					$new [] = $values [$ln];
			}
			return implode ( ', ', $new );
		} else {
			return __ ( 'информация отсутствует' );
		}
	}

	public function getFavorites() {

		$c = new Criteria ( );
		$c->add ( FavoritePeer::USER_ID, $this->id);
		//$c->addAnd ( FavoritePeer::PUB, 1);
		$cnt = FavoritePeer::doSelect ( $c );
		
		$arr=array();
		
		foreach ( $cnt as $cnt ) {
			$arr[$cnt->getFavId()]=$cnt->getsfGuardUserRelatedByFavId();
		}

		return $arr;
	}	


        ///получение баланса

 public function getBalance()
      {

            $c = new Criteria();
            $c->addSelectColumn('SUM(payment.komission)');
            $c->add(PaymentPeer::PARTNER_ID,$this->id);
            $c->add(PaymentPeer::LEVEL_ID,15,Criteria::NOT_EQUAL);
            $cnt = PaymentPeer::doCount($c);
            if($cnt==0)
            {
            $kom = 0;
            }else{
             $items = PaymentPeer::doSelectStmt($c);
             while($i = $items->fetch())
             {
                 $kom = $i[0];
             }
            }
            $c = new Criteria();
            $c->addSelectColumn('SUM(payment.summa)');
            $c->add(PaymentPeer::PARTNER_ID,$this->id);
            $c->add(PaymentPeer::LEVEL_ID,15);
            $cnt = PaymentPeer::doCount($c);
            if($cnt==0)
            {    
            $pay = 0;
            }else{         
             $items = PaymentPeer::doSelectStmt($c);   
             while($i = $items->fetch())
             {
                 $pay = $i[0];
             }
            }
             return $kom-$pay;

      }
	

      public function getCountPartnerUsers()
      {
           $c = new Criteria();
           $c->add(sfGuardUserPeer::PARTNER_ID,$this->id);
           $cnt = sfGuardUserPeer::doCount($c);
           return $cnt;
      }

      public function getCountPartnerGifts($id)
      {
           $c = new Criteria();
           $c->add(PaymentPeer::PARTNER_ID,$id);
           $c->add(PaymentPeer::LEVEL_ID,14);
           $cnt = PaymentPeer::doCount($c);
           return $cnt;
      }

      public function getCountPartnerMessages($id)
      {
           $c = new Criteria();
           $c->add(MailboxPeer::TO_PARTNER_ID,$id);
           $cnt = GiftboxPeer::doCount($c);
           return $cnt;
      }

            public function getCountPartnerVideoShow($id)
      {
           $c = new Criteria();
           $c->add(PaymentPeer::PARTNER_ID,$id);
           $c->add(PaymentPeer::LEVEL_ID,6);
           $cnt = PaymentPeer::doCount($c);
           return $cnt;
      }

            public function getCountVideos()
      {
           $c = new Criteria();
           $c->add(VideoPeer::USER_ID,$this->id);
           $cnt = VideoPeer::doCount($c);
           return $cnt;
      }

     public function save(PropelPDO $con = null)
      {
        $tag = new CacheTagUserById($this->getId());
        $tag->clean();
        parent::save($con);
      }

        public function delete(PropelPDO $con = null)
      {
        $tag = new CacheTagUserById($this->getId());
        $tag->clean();
        parent::delete($con);
      }

}
